#include "../main.h"

#define MAX_LEN 102400
/* 
--------------------------------------------------------------------------------
	函数功能：间隔发送心跳
--------------------------------------------------------------------------------
*/
int msg(void)
{
	int i;
	unsigned int nowtime, updatetime;

	sleep(2);

	updatetime = lasttime = nowtime = time((time_t *)NULL);

	while (1)
	{
		nowtime = time((time_t *)NULL);

		if (!strlen(DiskPath))
		{
			shell_cmd("cat /proc/mounts | grep '/dev/mmcblk' | awk '{print $2}'", DiskPath);
		}

		if (serverlink && !serverreg)
		{
			system_register(serverfd);
		}

		if (serverlink && serverreg)
		{
			if (nowtime - updatetime > 60 || !strlen(FCNIP))
			{
				updatetime = nowtime;
				system_fcnip();
			}

			system_ping(serverfd);
		}
		if (serverlink && nowtime - lasttime > TIMEOUT)
		{
			OUT_DEBUG(DEBUG, "SERVER TIMEOUT ");
			serverreg = 0;
			serverlink = 0;
			close(serverfd);
		}
		sleep(1);
	}
}

/* 
--------------------------------------------------------------------------------
	函数功能：解析命令行
--------------------------------------------------------------------------------
*/
int analyze_msg(int sockfd, char *msg, int length)
{
	int i, ret;
	char type[128];
	char clientId[128];
	char tagClientId[128];
	cJSON *pRoot = NULL, *pValue = NULL;
	pRoot = cJSON_Parse(msg);
	if (pRoot == NULL)
	{
		OUT_DEBUG(DEBUG, "JSON 数据解析失败 ! ");
		return 0;
	}

	memset(type, 0, sizeof(type));
	pValue = cJSON_GetObjectItem(pRoot, "type");
	if (pValue != NULL && pValue->type == cJSON_String)
	{
		memcpy(type, pValue->valuestring, sizeof(type));
	}

	memset(clientId, 0, sizeof(clientId));
	pValue = cJSON_GetObjectItem(pRoot, "clientId");
	if (pValue != NULL && pValue->type == cJSON_String)
	{
		memcpy(clientId, pValue->valuestring, sizeof(clientId));
	}

	memset(tagClientId, 0, sizeof(tagClientId));
	pValue = cJSON_GetObjectItem(pRoot, "tagClientId");
	if (pValue != NULL && pValue->type == cJSON_String)
	{
		memcpy(tagClientId, pValue->valuestring, sizeof(tagClientId));
	}
	char *content = NULL;
	pValue = cJSON_GetObjectItem(pRoot, "content");
	if (pValue != NULL && pValue->type == cJSON_String)
	{
		content = stringCopy(pValue->valuestring, strlen(pValue->valuestring));
	}
	// if (strcasecmp(tagClientId, RouteID)) //目标ID与本机ID不匹配
	// {
	// 	OUT_DEBUG(DEBUG, "### COMMON_DEVICE_KEY_ERROR ### ");
	// 	system_key_error(sockfd, clientId);
	// }
	else if (!strcmp(type, "REGISTER")) //注册包
	{
		OUT_DEBUG(DEBUG, "### REGISTER ### ");
		system_register_response(sockfd, clientId);
	}
	else if (!strcmp(type, "REGISTER_SUCCESS")) //注册成功返回
	{
		serverreg = 1;
		OUT_DEBUG(DEBUG, "### REGISTER_SUCCESS ### ");
	}
	else if (!strcmp(type, "COMMON_REGISTER_ERROR")) //注册失败返回
	{
		serverreg = 0;
		OUT_DEBUG(DEBUG, "### COMMON_REGISTER_ERROR ### ");
	}
	else if (!strcmp(type, "COMMON_ERROR")) //注册失败返回
	{
		OUT_DEBUG(DEBUG, "### COMMON_ERROR ### ");
	}
	else if (!strcmp(type, "PING")) //心跳
	{
		//		OUT_DEBUG(DEBUG,"### PING ### ");
		system_ping_response(sockfd, clientId);
	}
	else if (!strcmp(type, "PING_REPLY")) //心跳返回
	{
		//		OUT_DEBUG(DEBUG,"### PING_REPLY ### ");
	}
	else if (!strcmp(type, "DEVICE_PPPOE_SET")) //PPPOE设置
	{
		OUT_DEBUG(DEBUG, "### DEVICE_PPPOE_SET ### ");
		wan_set_pppoe(sockfd, clientId, content);
	}
	else if (!strcmp(type, "DEVICE_PPPOEGET_REQ")) //PPPOE获取
	{
		OUT_DEBUG(DEBUG, "### DEVICE_PPPOEGET_REQ ### ");
		wan_get_pppoe(sockfd, clientId);
	}
	else if (!strcmp(type, "DEVICE_NET_STATIC_SET")) //静态IP设置
	{
		OUT_DEBUG(DEBUG, "### DEVICE_NET_STATIC_SET ### ");
		wan_set_static(sockfd, clientId, content);
	}
	else if (!strcmp(type, "DEVICE_STATICGET_REQ")) //静态IP获取
	{
		OUT_DEBUG(DEBUG, "### DEVICE_STATICGET_REQ ### ");
		wan_get_static(sockfd, clientId);
	}
	else if (!strcmp(type, "DEVICE_NET_DYMIC_SET")) //动态IP设置
	{
		OUT_DEBUG(DEBUG, "### DEVICE_NET_DYMIC_SET ### ");
		wan_set_dhcp(sockfd, clientId, content);
	}
	else if (!strcmp(type, "DEVICE_DYNAMICGET_REQ")) //动态IP获取
	{
		OUT_DEBUG(DEBUG, "### DEVICE_DYNAMICGET_REQ ### ");
		wan_get_dhcp(sockfd, clientId);
	}
	else if (!strcmp(type, "DEVICE_TWOWIFI_SET")) //2.4G WIFI设置
	{
		OUT_DEBUG(DEBUG, "### DEVICE_TWOWIFI_SET ### ");
		wifi_set_ra0(sockfd, clientId, content);
	}
	else if (!strcmp(type, "DEVICE_TWOWIFIGET_REQ")) //2.4G WIFI获取
	{
		OUT_DEBUG(DEBUG, "### DEVICE_TWOWIFIGET_REQ ### ");
		wifi_get_ra0(sockfd, clientId);
	}
	else if (!strcmp(type, "DEVICE_FIVEWIFI_SET")) //5.8G WIFI设置
	{
		OUT_DEBUG(DEBUG, "### DEVICE_FIVEWIFI_SET ### ");
		wifi_set_rai0(sockfd, clientId, content);
	}
	else if (!strcmp(type, "DEVICE_FIVEWIFIGET_REQ")) //5.8G WIFI获取
	{
		OUT_DEBUG(DEBUG, "### DEVICE_FIVEWIFIGET_REQ ### ");
		wifi_get_rai0(sockfd, clientId);
	}
	else if (!strcmp(type, "DEVICE_CUSTOMERWIFI_SET")) //访客 WIFI设置
	{
		OUT_DEBUG(DEBUG, "### DEVICE_CUSTOMERWIFI_SET ### ");
		wifi_set_ra1(sockfd, clientId, content);
	}
	else if (!strcmp(type, "DEVICE_CUSTOMERWIFIGET_REQ")) //访客 WIFI获取
	{
		OUT_DEBUG(DEBUG, "### DEVICE_CUSTOMERWIFIGET_REQ ### ");
		wifi_get_ra1(sockfd, clientId);
	}
	else if (!strcmp(type, "DEVICE_NET_DEVICE_REQ")) //获取联网设备
	{
		OUT_DEBUG(DEBUG, "### DEVICE_NET_DEVICE_REQ ### ");
		device_get_list(sockfd, clientId);
	}
	else if (!strcmp(type, "DEVICE_BLACK_STATUS_SET")) //设置黑名单开关
	{
		OUT_DEBUG(DEBUG, "### DEVICE_BLACK_STATUS_SET ### ");
		device_set_black(sockfd, clientId, content);
	}
	else if (!strcmp(type, "DEVICE_BLACKLIST_REQ")) //获取黑名单列表
	{
		OUT_DEBUG(DEBUG, "### DEVICE_BLACKLIST_REQ ### ");
		device_get_blacklist(sockfd, clientId);
	}
	else if (!strcmp(type, "DEVICE_BLACKADD_SET")) //添加黑名单
	{
		OUT_DEBUG(DEBUG, "### DEVICE_BLACKADD_SET ### ");
		device_add_blacklist(sockfd, clientId, content);
	}
	else if (!strcmp(type, "DEVICE_BLACKREMOVE_SET")) //移除黑名单
	{
		OUT_DEBUG(DEBUG, "### DEVICE_BLACKREMOVE_SET ### ");
		device_del_blacklist(sockfd, clientId, content);
	}
	else if (!strcmp(type, "DEVICE_LIMITSPEED_REQ")) //获取联网设备限速
	{
		OUT_DEBUG(DEBUG, "### DEVICE_LIMITSPEED_REQ ### ");
		device_get_speedlimit(sockfd, clientId, content);
	}
	else if (!strcmp(type, "DEVICE_LIMITSPEED_SET")) //联网设备限速
	{
		OUT_DEBUG(DEBUG, "### DEVICE_LIMITSPEED_SET ### ");
		device_set_speedlimit(sockfd, clientId, content);
	}
	else if (!strcmp(type, "DEVICE_STATUS_UPANDDOWNSIZE_REQ")) //路由实时网速
	{
		OUT_DEBUG(DEBUG, "### DEVICE_STATUS_UPANDDOWNSIZE_REQ ### ");
		system_get_wanspeed(sockfd, clientId);
	}
	else if (!strcmp(type, "DEVICE_STATUS_CURRENT_REQ")) //当前网络
	{
		OUT_DEBUG(DEBUG, "### DEVICE_STATUS_CURRENT_REQ ### ");
		system_get_wantatus(sockfd, clientId);
	}
	else if (!strcmp(type, "DEVICE_LOADING_REQ")) //路由信息
	{
		OUT_DEBUG(DEBUG, "### DEVICE_LOADING_REQ ### ");
		system_info(sockfd, clientId);
	}
	else if (!strcmp(type, "DEVICE_DISK_SET")) //磁盘内存设置
	{
		OUT_DEBUG(DEBUG, "### DEVICE_DISK_SET ### ");
		disk_set_share(sockfd, clientId, content);
	}
	else if (!strcmp(type, "DEVICE_DISKGET_REQ")) //磁盘内存获取
	{
		//OUT_DEBUG(DEBUG,"### DEVICE_DISKGET_REQ ### ");
		disk_get_share(sockfd, clientId);
	}
	else if (!strcmp(type, "DEVICE_STORAGEITEM_DELETE_REQ")) //文件删除
	{
		OUT_DEBUG(DEBUG, "### DEVICE_STORAGEITEM_DELETE_REQ ### ");
		disk_delete_file(sockfd, clientId, content);
	}
	else if (!strcmp(type, "DEVICE_FILE_STORAGE_REQ")) //获取文件目录大小
	{
		OUT_DEBUG(DEBUG, "### DEVICE_FILE_STORAGE_REQ ### ");
		disk_size_file(sockfd, clientId);
	}
	else if (!strcmp(type, "DEVICE_VERSION_UPGRADE")) //版本更新
	{
		OUT_DEBUG(DEBUG, "### DEVICE_VERSION_UPGRADE ### ");
		device_version_upgrade(sockfd, clientId, content);
	}

	if (pRoot != NULL)
	{
		cJSON_Delete(pRoot);
	}
	if (content != NULL)
	{
		free(content);
		content = NULL;
	}
	return 0;
}

/* 
--------------------------------------------------------------------------------
	函数功能：组包,向服务端发送数据
--------------------------------------------------------------------------------
*/
int send_msg(int sockfd, char *data, int len)
{
	char *content = (char *)malloc(len + 4 * sizeof(int));
	int Msg_Head = htonl(MsgHead);
	int Msg_Ver = htonl(MsgVer);
	int File_Type = htonl(MsgType);
	int data_length = htonl(len);
	int offset = 0;
	memcpy(content, &Msg_Head, sizeof(int));
	offset += 4;
	memcpy(content + offset, &Msg_Ver, sizeof(int));
	offset += 4;
	memcpy(content + offset, &File_Type, sizeof(int));
	offset += 4;
	memcpy(content + offset, &data_length, sizeof(int));
	offset += 4;
	memcpy(content + offset, data, len);
	offset += len;

	int ret = send(sockfd, content, offset, 0);
	if (ret <= 0)
	{
		OUT_DEBUG(DEBUG, "Send MSG Error! ret=%d ", ret);
	}

	if (content != NULL)
	{
		free(content);
		content = NULL;
	}
	return ret;
}

/* 
--------------------------------------------------------------------------------
	函数功能：字符串分解
--------------------------------------------------------------------------------
*/
int stringcopy(char *dst, int dst_len, char *src, int src_len)
{
	int i = 0;

	for (i = 0; i < src_len; i++)
	{
		*src++;
	}

	for (i = 0; i < dst_len; i++)
	{
		*dst++ = *src++;
	}

	return 0;
}